package com.newcoder.community.util;

import com.newcoder.community.entity.User;
import org.springframework.stereotype.Component;

/**
 * @author edison
 * @version 2023
 * @date 2024/1/21 21:25
 */

/**
 * 持有用户信息,用于代替session对象
 */
@Component
public class HostHolder {

    private ThreadLocal<User> users = new ThreadLocal<>();

    // 将用户存入ThreadLocal,实现了多线程的隔离
    public void setUser(User user) {
        users.set(user);
    }

    // 从ThreadLocal中取出用户,实现了多线程的隔离
    public User getUser() {
        return users.get();
    }

    // 清理ThreadLocal中的用户
    public void clear() {
        users.remove();
    }

}
